import BaseController from '@/lib/base-controller';
import { GET, POST, RequestMapping } from '@/lib/add-router';
// import auth from '@/lib/auth'

@RequestMapping('/api/menu')
export default class MenuController extends BaseController {
  @POST('/add')
  async add() {
    try {
      const { ctx, service } = this;

      // const { name, description } = ctx.request.body;
      const menu = await service.menu.addMenu(ctx.request.body);
      this.success({
        code: 0,
        msg: '添加成功',
        menu,
      });
    } catch (e) {
      this.fail({
        code: 1,
        message: e,
      });
    }
  }

  @POST('/update')
  async update() {
    try {
      const { ctx, service } = this;
      ctx.validate({
        id: 'number',
      });
      //  const { id, name, description } = ctx.request.body;
      const menu = await service.menu.updateMenu(ctx.request.body);
      this.success({
        code: 0,
        msg: '修改成功',
        menu,
      });
    } catch (e) {
      this.fail({
        code: 1,
        message: e,
      });
    }
  }

  @POST('/delete')
  async delete() {
    try {
      const { ctx, service } = this;

      // const { id } = ctx.request.body;
      const menu = await service.menu.deleteMenu(ctx.request.body);
      this.success({
        code: 0,
        msg: '删除成功',
        menu,
      });
    } catch (e) {
      this.fail({
        code: 1,
        message: e,
      });
    }
  }
  @POST('/queryByRole')
  async RoleMenuList() {
    try {
      const { ctx, service } = this;
      ctx.validate({
        id: 'number',
      });
      // const { id } = ctx.request.body;
      const menu = await service.menu.getMenuListByRoleId(ctx.request.body);
      this.success({
        code: 0,
        msg: '查询成功',
        menu,
      });
    } catch (err) {
      this.fail({
        code: 1,
        message: err,
      });
    }
  }
  // MenuListByUserId
  @POST('/queryByUserId')
  async MenuListByUserId() {
    try {
      const { ctx, service } = this;
      ctx.validate({
        id: 'number',
      });
      // const { id } = ctx.request.body;
      const menu = await service.menu.getMenuListByUserId(ctx.request.body);
      this.success({
        code: 0,
        msg: '查询成功',
        menu,
      });
    } catch (e) {
      this.fail({
        code: 1,
        message: e,
      });
    }
  }

  @GET('/list')
  async list() {
    try {
      const { service } = this;
      const menu = await service.menu.getMenuList();
      this.success({
        code: 0,
        msg: '获取成功',
        menu,
      });
    } catch (e) {
      this.fail({
        code: 1,
        message: e,
      });
    }
  }
}
